package com.flycms.module.cms.service;

import com.flycms.core.entity.PageVo;
import com.flycms.module.cms.dao.OrderDao;
import com.flycms.module.cms.model.Order;
import com.flycms.module.cms.param.NewOrder;
import com.flycms.module.cms.param.OrderParam;
import com.flycms.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huangzt
 * @version 1.0
 * @date 2020/3/2 17:06
 */
@Service(value = "cmsOrderService")
public class CmsOrderService {
    @Autowired
    OrderDao orderDao;

    public int insert(Order order){
        return orderDao.insertSelective(order);
    }

    public Order queryByPK(long id){
        return orderDao.queryByPk(id);
    }

    public PageVo<Order> queryByPage(OrderParam param){
        //統計
        int count = orderDao.count(param);
        if(count <= 0){
            return new PageVo<>(param.getPageIndex());
        }
        List<Order> orders =
                orderDao.page(param, (param.getPageIndex()-1) * param.getPageSize(), param.getPageSize());
        return new PageVo<>(param.getPageIndex(),count,orders);
    }

    /**
     * 确认发货
     * @param id 订单ID
     */
    public void send(Long id) {
        orderDao.send(id);
    }

    /**
     * 新订单保存
     * @param order 订单信息
     */
    public void insert(NewOrder order) {
        Order ord = ConvertUtils.sourceToTarget(order, Order.class);
        orderDao.insertSelective(ord);
    }
}
